package com.tmc.platform.ctrip;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.constant.FareConstant.Platform;
import com.tmc.entity.common.EthInterfaceAccount;
import com.tmc.entity.intair.IntairOrder;
import com.tmc.platform.ctrip.bean.OpenIssueBillInfoResponse;
import com.tmc.platform.ctrip.bean.OpenIssueBillResponse;
import com.tmc.platform.ctrip.bean.OpenIssueBillSearchResult;
import com.tmc.platform.ctrip.bean.OpenIssueOrderListResponse;
import com.tmc.platform.ctrip.bean.OpenSearchIssueBillResponse;
import com.tmc.platform.ctrip.util.CtripOrderUtil;
import com.tmc.platform.ctrip.util.XStreamUtil;
import com.tmc.service.intair.IntairOrderServiceImpl;
import com.tmc.util.DateUtil;
import com.tmc.util.ObjectUtil;
import com.tmc.util.XMLUtil;

@Service
public class CtripOrderServiceImpl {
	
	private Log logger = LogFactory.getLog(CtripOrderServiceImpl.class);
	
	@Autowired
	private CtripServiceImpl service;
	
	@Autowired
	private IntairOrderServiceImpl orderSer;
	
	public Platform getPlatForm() {
		return Platform.ctrip;
	}
	
	public EthInterfaceAccount getAccount() {
		
		return null;
	}

	public CommonResult syncOrdersFromPlatform(ContextInfo context, String orderBeginDateTime, String orderEndDateTime) {
		List<IntairOrder> orders = new ArrayList<IntairOrder>();
		try {
			Date beginDate = DateUtil.format(orderBeginDateTime,"yyyy-MM-dd HH:mm:ss");
			Date endDate = DateUtil.format(orderEndDateTime, "yyyy-MM-dd HH:mm:ss");
			String resp = new String(service.openIssueOrderList(beginDate,endDate).getBytes(),"utf-8");
			String xml = XMLUtil.getPartXmlString("OpenIssueOrderListResponse",resp);
			OpenIssueOrderListResponse response = XStreamUtil.fromXml(xml);
			if (response == null || response.getIssueOrderList() == null || response.getIssueOrderList().isEmpty()) {
				return CommonResult.makeSuccess();
			}
			for (OpenIssueBillResponse ctripOrder : response.getIssueOrderList()) {
				IntairOrder order = CtripOrderUtil.toSysOrder(ctripOrder);
				orderSer.beforeSave(order);
				orderSer.save(context, order);
				orders.add(order);
			}
		} catch (Exception e) {
			logger.error("同步出错!", e);
			return CommonResult.makeFail("同步错误！", e.getMessage());
		}
		return CommonResult.makeSuccess("", orders.size());
	}
	
	public CommonResult syncHistoryOrdersFromPlatform(ContextInfo context, String orderBeginDateTime, String orderEndDateTime, Long issueBillID, String issueStatus) {
		List<IntairOrder> orders = new ArrayList<IntairOrder>();
		String resp = "";
		try{
			Date beginDate = DateUtil.format(orderBeginDateTime,"yyyy-MM-dd HH:mm:ss");
			Date endDate = DateUtil.format(orderEndDateTime,"yyyy-MM-dd HH:mm:ss");
			resp =new String(service.searchIssueBill(beginDate, endDate, issueBillID, issueStatus).getBytes(),"utf-8"); 
			String xml = resp.substring(resp.indexOf("<OpenSearchIssueBillResponse>"), resp.indexOf("</OpenSearchIssueBillResponse>") + 30);
			OpenSearchIssueBillResponse response = XStreamUtil.fromXml(xml);
			if (response == null || response.getOpenIssueBillSearchResultList() == null) {
				return CommonResult.makeSuccess();
			}
			for (OpenIssueBillSearchResult  result : response.getOpenIssueBillSearchResultList()) {
				Long issueBillId = result.getIssueBillID();
				System.out.println("处理issueBillId" + issueBillId);
				String detailResponse = service.issueBillInfo(issueBillId);
				if (ObjectUtil.noText(detailResponse)) {
					continue;
				}
				System.out.println(detailResponse);
				detailResponse = detailResponse.substring(detailResponse.indexOf("<OpenIssueBillInfoResponse>"), detailResponse.indexOf("</OpenIssueBillInfoResponse>") + 30);
				OpenIssueBillInfoResponse openIssueBillInfoResponse = XStreamUtil.fromXml(detailResponse);
				IntairOrder order = CtripOrderUtil.toSysOrder(openIssueBillInfoResponse);
				orderSer.save(context, order);
				orders.add(order);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return CommonResult.makeSuccess("", orders.size());
	}

	public CommonResult syncOrdersToPlatform(List<IntairOrder> orders) {

		return null;
	}

}
